import { Component } from 'react'
import store from '../store'
import { DECREASE } from '../store/actionType'

export default class Sub extends Component {
	constructor(props) {
		super(props)
		this.state = store.getState()
		console.log('sub组件store订阅(subscribe)方法storeChange...')
		store.subscribe(this.storeChange)
	}

	storeChange = () => {
		console.log('sub组件触发subscribe方法...')
		this.setState(store.getState())
	}

	onDecrease = count => {
		store.dispatch({ type: DECREASE, count })
	}

	render() {
		return (
			<div>
				{this.state.count}
				<button onClick={() => this.onDecrease(2)}>减少</button>
			</div>
		)
	}
}
